package com.jucai.pppeiyouclient.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.UserInfoBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.MyRatingBar;

import org.json.JSONObject;

import java.util.HashMap;

import de.hdodenhof.circleimageview.CircleImageView;

public class EvaluateActivity extends BaseActivity {
    private static final String TAG = "EvaluateActivity : ";
    private CircleImageView evalImg;
    private TextView evalNick, evalComplaint, evalSubmit;
    private EditText evalContent;
    private ImageView evalAddPic;
    private MyRatingBar ratingService, ratingAttitude, ratingRp;
    private UserInfoBean user;


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_evaluate);
        ((TextView) findViewById(R.id.common_title_text)).setText("评价");
        evalImg = (CircleImageView) findViewById(R.id.eval_img);
        evalNick = (TextView) findViewById(R.id.eval_nick);
        evalComplaint = (TextView) findViewById(R.id.eval_complaint);
        evalSubmit = (TextView) findViewById(R.id.eval_submit);
        evalContent = (EditText) findViewById(R.id.eval_content);
        evalAddPic = (ImageView) findViewById(R.id.eval_add_pic);
        ratingService = (MyRatingBar) findViewById(R.id.rating_service);
        ratingAttitude = (MyRatingBar) findViewById(R.id.rating_attitude);
        ratingRp = (MyRatingBar) findViewById(R.id.rating_rp);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Conductor_Activity.getuserinfo");
        param.put("id", getIntent().getStringExtra("receiver_id"));
        param.put("member_id", LoginUtils.getMember_id(this));
        param.put("token", LoginUtils.getToken(this));

        CommonRequestNetData.getJsonObjectData(EvaluateActivity.this, param, true, new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) {
                LogUtils.e(TAG + "jsObj = " + jsonObject);
                user = new Gson().fromJson(jsonObject.toString(), UserInfoBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(EvaluateActivity.this)
                                .load(user.getMember_avatar())
                                .override(60, 60)
                                .centerCrop()
                                .into(evalImg);

                        evalNick.setText(user.getMember_nickname());
                    }
                });
            }
        });
    }

    public static void startOrderEvaluate(Context context, String orderSn, String receiverId, String uId, String orderId) {
        Intent intent = new Intent(context, EvaluateActivity.class);
        intent.putExtra("order_sn", orderSn);
        intent.putExtra("receiver_id", receiverId);
        intent.putExtra("u_id", uId);
        intent.putExtra("order_id", orderId);
        context.startActivity(intent);
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        evalComplaint.setOnClickListener(this);
        evalAddPic.setOnClickListener(this);
        evalSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
//                ActivityCollector.removeActivity(this);
                CommonH5Activity.startCommH5(EvaluateActivity.this, 1,
                        SharedPreferencesUtil.getStringPreference(EvaluateActivity.this, "HISTORY", "his_url", ""),
                        null, getIntent().getStringExtra("order_id"), null);
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(EvaluateActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.eval_complaint:
                ComplaintActivity.startComplaint(EvaluateActivity.this, getIntent().getStringExtra("order_sn"),
                        getIntent().getStringExtra("receiver_id"), getIntent().getStringExtra("u_id"));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.eval_add_pic:
                break;

            case R.id.eval_submit:
                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Client_Evaluate.a_evaluateinto");
                param.put("torder_ordersn", getIntent().getStringExtra("order_sn"));
                param.put("recevier_id", getIntent().getStringExtra("receiver_id"));
                if (!TextUtils.isEmpty(evalContent.getText().toString())) {
                    param.put("tevaluate_content", evalContent.getText().toString());
                }else{
                    ToastUtils.showToast("请先输入评价内容！");

                }
                param.put("tevaluate_score_service", ratingService.getRating() + "");
                param.put("tevaluate_score_attitude", ratingAttitude.getRating() + "");
                param.put("tevaluate_score_character", ratingRp.getRating() + "");
                param.put("uid", getIntent().getStringExtra("u_id"));
                param.put("member_id", LoginUtils.getMember_id(this));
                param.put("token", LoginUtils.getToken(this));

                LogUtils.e(TAG + "param = " + param.toString());

                CommonRequestNetData.getJsonStringData(EvaluateActivity.this, param, true, new CommonRequestNetData.JsonStringCallBack() {
                    @Override
                    public void parseJsonString(String str) {
                        LogUtils.e(TAG + "str = " + str);
                        ToastUtils.showToast(str);

                        CommonH5Activity.startCommH5(EvaluateActivity.this, 1,
                                SharedPreferencesUtil.getStringPreference(EvaluateActivity.this, "HISTORY", "his_url", ""),
                                null, getIntent().getStringExtra("order_id"), null);
                        ActivityCollector.removeActivity(EvaluateActivity.this);
                    }
                });

                break;
        }
    }
}
